package com.taptech.doufu.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.weex.image.TFImageViewUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.adapter.ScrollAbleViewPagerAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.dialog.CpAddContentDialog;
import com.taptech.doufu.ui.fragment.CommonCpFragment;
import com.taptech.doufu.bean.ShareBeansInfo;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.ui.view.PersonalTabViewGroup;
import com.taptech.doufu.bean.personalcenter.SweepFlowerBean;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.view.ScrollableLayout;
import com.taptech.doufu.ui.fragment.ScrollAbleFragment;
import com.taptech.doufu.services.CollectService;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.ui.adapter.HorizontalListViewAdapter;
import com.taptech.doufu.ui.view.HorizontalListView;
import com.taptech.doufu.util.AnimationUtil;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.SharePicPopupWindow;
import com.taptech.doufu.ui.view.WaitDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import androidx.viewpager.widget.ViewPager;

public class NewCpDesActivity extends DiaobaoBaseActivity implements OnClickListener, HttpResponseListener
{
    public final static int HANDLE_LOAD_REPORT_INFO = 2000;

    public static final int TYPE_CP = 1;
    public static final int TYPE_TAG = TYPE_CP + 1;
    private PersonalTabViewGroup mTabView;
    private List<ScrollAbleFragment> mSweepListViews;
    private TTHomeViewPager mViewPager;
    private ScrollAbleViewPagerAdapter viewPagerAdapter;
    private String NAMESTRING[] = new String[]{"小说", "绘画", "文评", "日常"};
    private ImageView cp_bac_iv;
    private View personal_bac, order_ly;
    private RelativeLayout mTopLayout;
    private TextView cp_name, tag_frist_tv;
    private ImageView cp_image;
    private TextView cp_flowernum_tv, content_num_tv;
    private HorizontalListView cp_flowerUserList;
    private ImageView cp_top_more;
    private Button order_bt_cp, give_flower_bt, order_bt_tag;
    private CpBean cpBean;
    private List<SweepFlowerBean> flowerUserListDatas;
    private String last = "";
    private ScrollableLayout mScrollLayout;
    private View flower_ly;
    int type;
    String tag_id, name;
    private WaitDialog waitDialog;
    private HorizontalListViewAdapter flowerUserAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_tag_des_test);
        type = getIntent().getIntExtra(Constant.TYPE, 0);
        waitDialog = new WaitDialog(this, R.style.loadNovelDialog, "");
        findViewByID();
        loadData();
    }

    private void initViewByType()
    {
        switch (type)
        {
            case TYPE_CP:
                flower_ly.setVisibility(View.VISIBLE);
                give_flower_bt.setVisibility(View.VISIBLE);
                // order_bt_cp.setVisibility(View.GONE);
                //give_flower_bt.setVisibility(View.GONE);
                break;
            case TYPE_TAG:
                //flower_ly.setVisibility(View.GONE);
                //order_bt_tag.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void loadData()
    {
        cpBean = new CpBean();
        tag_id = getIntent().getStringExtra(Constant.TOPIC_ID);
        name = getIntent().getStringExtra(Constant.KEY_SEARCH_TAG);
        if (tag_id != null || name != null)
        {
            last = "";
            waitDialog.show();
            CpService.getInstance().loadSingleCpInfo(NewCpDesActivity.this, tag_id, name);
        }

    }

    private void findViewByID()
    {
        tag_frist_tv = (TextView) findViewById(R.id.tag_frist_tv);
        flower_ly =  findViewById(R.id.flower_ly);
        mTopLayout = (RelativeLayout) findViewById(R.id.head_title_layout);
        cp_bac_iv = (ImageView) findViewById(R.id.head_title_imageview);
        personal_bac =  findViewById(R.id.personal_bac);
        cp_image = (ImageView) findViewById(R.id.cp_image);
        cp_name = (TextView) findViewById(R.id.cp_name);
        cp_flowernum_tv = (TextView) findViewById(R.id.flower_num_tv);
        content_num_tv = (TextView) findViewById(R.id.content_num_tv);
        cp_top_more = (ImageView) findViewById(R.id.cp_top_more);
        give_flower_bt = (Button) findViewById(R.id.give_flower_bt);
        order_bt_cp = (Button) findViewById(R.id.order_bt_cp);
        order_bt_tag = (Button) findViewById(R.id.order_bt_tag);
        order_ly = (View) findViewById(R.id.order_ly);
        cp_flowerUserList = (HorizontalListView) findViewById(R.id.flower_rank_listview);
        personal_bac.setOnClickListener(this);
        cp_top_more.setOnClickListener(this);
        order_bt_cp.setOnClickListener(this);
        give_flower_bt.setOnClickListener(this);
        order_bt_tag.setOnClickListener(this);

        mScrollLayout = (ScrollableLayout) findViewById(R.id.scrollableLayout);

    }

    protected void initUpView()
    {
        if (cpBean == null)
        {
            return;
        }
        mTabView = (PersonalTabViewGroup) findViewById(R.id.top_tab_view);
        switch (type)
        {
            case TYPE_CP:
            case TYPE_TAG:
//                NAMESTRING = new String[]{"日常", "绘画", "小说"};
                NAMESTRING = new String[]{"小说"};
                break;
        }
        mTabView.putStringsTabs(NAMESTRING);
        mTabView.setChoosePageListener(new PersonalTabViewGroup.ChoosePage()
        {
            @Override
            public void updatePage(int position)
            {
                mViewPager.setCurrentItem(position, true);
            }
        });
        mViewPager = (TTHomeViewPager) findViewById(R.id.sweep_view_pager);
        mViewPager.setOffscreenPageLimit(2);

        // mViewPager.ViewFlowListener(false);
        initFragmentList();
        viewPagerAdapter = new ScrollAbleViewPagerAdapter(getSupportFragmentManager(), mSweepListViews);
        mViewPager.setAdapter(viewPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int arg0)
            {
                // TODO Auto-generated method stub
                mTabView.moveTopStripTo(arg0);
                mScrollLayout.getHelper().setCurrentScrollableContainer(mSweepListViews.get(arg0));
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub
            }

        });

    }

    private void initFragmentList()
    {
        mSweepListViews = new ArrayList<>();
        if(((tag_id==null)||(tag_id.length()>0))&&(cpBean!=null)){
            tag_id=cpBean.getId();
        }
        switch (type)
        {
            case TYPE_CP:
                mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_SINGLE_CP_NOVEL, cpBean.getName()));
//                mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_SINGLE_CP_DRAW, cpBean.getName()));
//                mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_SINGLE_CP_NOTE, tag_id));
                break;
            case TYPE_TAG:
//                mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_SINGLE_CP_NOTE, tag_id));
//                mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_SINGLE_TAG_DRAW, cpBean.getName()));
                mSweepListViews.add(CommonCpFragment.newInstance(CommonCpFragment.TYPE_SINGLE_TAG_NOVEL, cpBean.getName()));
                break;
        }
        mScrollLayout.getHelper().setCurrentScrollableContainer(mSweepListViews.get(0));


    }

    public void setCpInfo()
    {
        if (cpBean != null)
        {
            flowerUserListDatas = new ArrayList<SweepFlowerBean>();
            if (cpBean.getUsers() != null)
            {
                flowerUserListDatas.addAll(cpBean.getUsers());
            }
            flowerUserAdapter = new HorizontalListViewAdapter(this, flowerUserListDatas, HorizontalListViewAdapter.TYPE_CP_DES, false);
            cp_flowerUserList.setAdapter(flowerUserAdapter);
            if (cpBean.getName().length() > 12)
            {
                cp_name.setTextSize(15);
            }
            cp_name.setText(cpBean.getName());
            content_num_tv.setText(cpBean.getCount());
            cp_flowernum_tv.setText(cpBean.getFlower_count());
            if (("18").equals(cpBean.getType_id()))
            {
                type = TYPE_CP;
            } else
            {
                type = TYPE_TAG;
            }
            switch (type)
            {
                case TYPE_CP:
                    cp_image.setImageResource(R.drawable.cp_deaf_iv);
                    break;
                case TYPE_TAG:
                    cp_image.setImageResource(R.drawable.cp_defult_iv_bac);
                    cp_bac_iv.setImageResource(R.drawable.cp_defult_iv_bac);
                    break;
            }
            if (cpBean.getIcon() != null && !cpBean.getIcon().equals(""))
            {
//                ImageManager.displayImage(cp_image, cpBean.getIcon(), 0);
                TFImageViewUtil.showVagueImage(cp_bac_iv,cpBean.getIcon(),5);
            } else
            {
                switch (type)
                {
                    case TYPE_CP:
                        cp_image.setImageResource(R.drawable.cp_deaf_iv);
                        TFImageViewUtil.showVagueImage(cp_bac_iv,cpBean.getIcon(),5);
                        break;
                    case TYPE_TAG:
                        cp_image.setImageResource(R.drawable.cp_defult_iv_bac);
                        TFImageViewUtil.showVagueImage(cp_bac_iv,cpBean.getIcon(),5);
                        tag_frist_tv.setVisibility(View.VISIBLE);
                        tag_frist_tv.setText(cpBean.getName().substring(0, 1));
                        break;
                }
            }
            initViewByType();
            setFlowerUserView();
        }
    }

    private void setFlowerUserView()
    {
        if (cpBean.getFollowing().equals("1"))
        {
            order_bt_cp.setVisibility(View.GONE);
            cp_flowerUserList.setVisibility(View.VISIBLE);
        } else
        {
            order_bt_cp.setVisibility(View.VISIBLE);
            cp_flowerUserList.setVisibility(View.GONE);

        }
        flowerUserAdapter.notifyDataSetChanged();
    }

    View cacel_line;
    LinearLayout collectLy;
    View mMoreView;
    private void makeTopMorePopupWindow()
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMoreView = inflater.inflate(R.layout.popup_reader_more_panel, null);
        View lay = mMoreView.findViewById(R.id.lay);
        lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMorePopupWindow != null) {
                    mMorePopupWindow.dismiss();
                }
            }
        });
        LinearLayout reportLy = (LinearLayout) mMoreView.findViewById(R.id.popup_reader_more_report);
        reportLy.setVisibility(View.VISIBLE);
        mMoreView.findViewById(R.id.popup_reader_more_report_line).setVisibility(View.VISIBLE);
        collectLy = (LinearLayout) mMoreView.findViewById(R.id.popup_reader_more_collect);
        TextView cancel_tv = (TextView) mMoreView.findViewById(R.id.popup_reader_more_tv_collect);
        cacel_line = mMoreView.findViewById(R.id.popup_reader_more_fav_line);
        cacel_line.setVisibility(View.VISIBLE);
        cancel_tv.setText("取消订阅");
        reportLy.setOnClickListener(this);
        collectLy.setOnClickListener(this);


        mMorePopupWindow = new PopupWindow(mMoreView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mMorePopupWindow.setAnimationStyle(R.style.Animation_Right_Left);
        mMorePopupWindow.setBackgroundDrawable(new BitmapDrawable());

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.order_bt_cp:
            case R.id.order_bt_tag:
            {
                if (!AccountService.getInstance().isLogin())
                {
                    //AccountService.getInstance().jumpToLogin();
                    Toast.makeText(this, "请先登录噢！", Toast.LENGTH_SHORT).show();
                    return;
                }
                waitDialog.show();
                switch (type)
                {
                    case TYPE_CP:
                        CpService.getInstance().addCp(cpBean.getId(), this);
                        CpService.getInstance().recordSearchCp(this, cpBean.getName(), cpBean.getName());
                        TMAnalysis.event(this, "cp_order_onclick");
                        break;
                    case TYPE_TAG:
                        CollectService.getInstance().addCollectTagRequest(this, cpBean.getName());
                        TMAnalysis.event(this, "tag_order_onclick");
                        break;
                }
                break;
            }

            case R.id.give_flower_bt:
            {
                if (!AccountService.getInstance().isLogin())
                {
                    //AccountService.getInstance().jumpToLogin();
                    Toast.makeText(this, "请先登录噢！", Toast.LENGTH_SHORT).show();
                    return;
                }
                makeGiveFlowerPopupWindow();
                break;
            }
            // 个人动态
            case R.id.cp_top_more:
            {
                showMorePopupWindow();
            }
            break;
            case R.id.personal_bac:
                finish();
                break;
            case R.id.give_flower_btn:
                if (!AccountService.getInstance().isLogin())
                {
                    //AccountService.getInstance().jumpToLogin();
                    Toast.makeText(this, "请先登录噢！", Toast.LENGTH_SHORT).show();
                    return;
                }
                clickGiveFlowering();
                TMAnalysis.event(this, "cp_give_flower");
                break;
            // 查看详细送花列表
            case R.id.flower_sum:
                Intent rankFlower = new Intent(NewCpDesActivity.this, FlowerRankActivity.class);
                rankFlower.putExtra(Constant.OBJECT_TYPE, Constant.OBJECT_TYPE_CP + "");
                rankFlower.putExtra(Constant.NOTE_ID, cpBean.getId());
                startActivity(rankFlower);
                break;
            case R.id.popu_cancel_bt:
                if (makeGiveFlowerPopupWindow != null && makeGiveFlowerPopupWindow.isShowing())
                {
                    makeGiveFlowerPopupWindow.dismiss();
                    setFlowerUserView();
                }
                break;
            case R.id.popup_reader_more_report:
                Toast.makeText(this, "举报成功！", Toast.LENGTH_SHORT).show();
                break;
            case R.id.popup_reader_more_collect:
                if (!AccountService.getInstance().isLogin())
                {
                    //AccountService.getInstance().jumpToLogin();
                    Toast.makeText(this, "请先登录噢！", Toast.LENGTH_SHORT).show();
                    return;
                }
                waitDialog.show();
                switch (type)
                {
                    case TYPE_CP:
                        CpService.getInstance().cancelCp(cpBean.getId(), NewCpDesActivity.this);
                        TMAnalysis.event(this, "cp_cancel_order");
                        break;
                    case TYPE_TAG:
                        CollectService.getInstance().removeCollectTagRequest(this, cpBean.getName());
                        TMAnalysis.event(this, "tag_cancel_order");
                        break;
                }
                break;
            default:
                break;
        }

    }

    private CpAddContentDialog addContentPopuWindow;

    private void makeAddContentPopupWindow()
    {
        if (cpBean.getName() == null)
        {
            return;
        }
        addContentPopuWindow = new CpAddContentDialog(this, R.style.UgcSweepDialog, CpAddContentDialog.TYPE_CPDES);
        addContentPopuWindow.setTagString(cpBean);
        addContentPopuWindow.show();
        android.view.WindowManager.LayoutParams lp = addContentPopuWindow.getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        addContentPopuWindow.getWindow().setAttributes(lp);
    }

    PopupWindow makeGiveFlowerPopupWindow;

    private void makeGiveFlowerPopupWindow()
    {
        if (makeGiveFlowerPopupWindow == null)
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View flowerView = inflater.inflate(R.layout.popup_cp_give_flower, null);
            initFlowerView(flowerView);
            makeGiveFlowerPopupWindow = new PopupWindow(flowerView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
            makeGiveFlowerPopupWindow.setAnimationStyle(R.style.Animation_Bottom_Top);
            makeGiveFlowerPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            makeGiveFlowerPopupWindow.setFocusable(true);
            makeGiveFlowerPopupWindow.setOutsideTouchable(true);
        }
        makeGiveFlowerPopupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER, 0, 0);
        makeGiveFlowerPopupWindow.update();

    }

    //献花动画
    Animation flowerAnimation;
    private com.taptech.doufu.ui.view.RoundImageView flower, flowerIngUserImg;
    private ImageView[] flower_copy = new ImageView[8];
    //献花动画的状态
    private static final int REFRESH_FLOWER_STATUS = -4;
    private static final int RESET_CLICK_FLOWER = -3;
    private static final int SEND_FLOWER_FAIL = -2;
    private static final int SEND_FLOWER_SUCCESS = -1;
    private static final int SEND_FLOWER_TIME = 4;
    private int flower_count = 0;
    private int count = 0;
    private int flower_img_position = 8;
    private Timer timer;
    boolean isFlowerClick = false;
    /**
     * 献花按钮是否可献花，献花完后有3秒的冷却时间
     */
    private boolean flowerClickable = true;
    int flowerSum = 0;

    //献花数量提示
    private TextView flowerStatusTips;
    //开始献花的文字提示
    private TextView startFlowerTips;
    //无人献花时的提示
    private TextView no_flower;
    //日志献花总数
    private TextView flower_sum;
    //献花列表
    private HorizontalListView flowerUserList;
    //献花需要的控件初始化
    private HorizontalListViewAdapter popu_flowerUserAdapter;

    private void initFlowerView(View view)
    {
        //献花
        //douziLackDialogInitView(view);
        flower = (com.taptech.doufu.ui.view.RoundImageView) view.findViewById(R.id.give_flower_btn);
        view.findViewById(R.id.popu_cancel_bt).setOnClickListener(this);
        flower.setOnClickListener(this);
        flowerIngUserImg = (com.taptech.doufu.ui.view.RoundImageView) view.findViewById(R.id.flower_ing_userimg);
        flowerIngUserImg.setImageResource(R.drawable.cp_deaf_iv);
        if (cpBean != null)
        {
            ImageManager.displayImage(flowerIngUserImg, cpBean.getIcon(), 0);
        }
        no_flower = (TextView) view.findViewById(R.id.no_flower);
        flowerStatusTips = (TextView) view.findViewById(R.id.flower_ing_status);
        startFlowerTips = (TextView) view.findViewById(R.id.flower_ing_tips);
        startFlowerTips.setText("CP要人气，戳菊请用力");
        flower_sum = (TextView) view.findViewById(R.id.flower_sum);
        if (cpBean != null)
        {
            flower_sum.setText(cpBean.getFlower_count() + "");
        }
        flower_sum.setOnClickListener(this);
        //献花列表
        flowerUserList = (HorizontalListView) view.findViewById(R.id.flower_rank_listview);
        popu_flowerUserAdapter = new HorizontalListViewAdapter(this, flowerUserListDatas, HorizontalListViewAdapter.TYPE_NORMAL, true);
        flowerUserList.setAdapter(popu_flowerUserAdapter);

        flowerAnimation = AnimationUtils.loadAnimation(this, R.anim.sweep_flower_rotate);
        LinearInterpolator lin = new LinearInterpolator();
        flowerAnimation.setInterpolator(lin);
        try
        {
            for (int i = 1; i < 9; i++)
            {
                Field fieldLayout = R.id.class.getDeclaredField("sweep_flower_copy_" + i);
                flower_copy[i - 1] = (ImageView) view.findViewById(Integer.parseInt(fieldLayout.get(null).toString()));
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    // 缺豆子献花弹窗初始化
    // 缺豆子献花失败时的ui
    RelativeLayout getDouziLayout;
    TextView douziTextDes;
    TextView douziDesBtn, shareBtn;
    ImageView closeWindowBtn;

    // private RelativeLayout sharePopupWindow;

    private void douziLackDialogInitView(View view)
    {
        View.OnClickListener onClickListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                switch (v.getId())
                {
                    case R.id.jump_to_douzi_des_btn:
                        String url = "http://www.doufu.la/static/score/rule.html";
                        Intent intent = new Intent();
                        intent.setClass(NewCpDesActivity.this, EasyBrowseActivity.class);
                        intent.putExtra(Constant.URL, url);
                        intent.putExtra(Constant.TITLE, "豆子攻略");
                        startActivity(intent);
                        break;
                    case R.id.jump_to_share_btn:
                        if (makeGiveFlowerPopupWindow != null && makeGiveFlowerPopupWindow.isShowing())
                        {
                            makeGiveFlowerPopupWindow.dismiss();
                        }
                        getDouziLayout.setVisibility(View.GONE);
                        SharePicPopupWindow menuWindow = new SharePicPopupWindow(NewCpDesActivity.this);
                        ShareBeansInfo shareInfo = new ShareBeansInfo(ShareBeansInfo.OWN_SHARE_FLAG);
                        menuWindow.setShareBeans(shareInfo);
                        // 显示窗口
                        menuWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
                        break;
                    case R.id.close_btn:
                        getDouziLayout.setVisibility(View.GONE);
                        break;
                }
            }
        };
        // sharePopupWindow = (RelativeLayout) view.findViewById(R.id.share_window_layout);
        getDouziLayout = (RelativeLayout) view.findViewById(R.id.get_douzi_dialog_layout);
        douziTextDes = (TextView) view.findViewById(R.id.text_des_content);
        douziDesBtn = (TextView) view.findViewById(R.id.jump_to_douzi_des_btn);
        shareBtn = (TextView) view.findViewById(R.id.jump_to_share_btn);
        closeWindowBtn = (ImageView) view.findViewById(R.id.close_btn);
        douziDesBtn.setOnClickListener(onClickListener);
        shareBtn.setOnClickListener(onClickListener);
        closeWindowBtn.setOnClickListener(onClickListener);
    }
    //点击献花时的动画呈现

    private void clickGiveFlowering()
    {
        if (flowerClickable)
        {
            // 定义计时器
            if (!isFlowerClick)
            {
                isFlowerClick = true;
                flower.startAnimation(flowerAnimation);
                flowerStatusTips.setVisibility(View.VISIBLE);
                flowerIngUserImg.setVisibility(View.VISIBLE);
                AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
                alpha.setDuration(1000);
                alpha.setFillAfter(true);
                flowerIngUserImg.startAnimation(alpha);
                flowerStatusTips.startAnimation(alpha);
                flower_copy[flower_img_position % 8].setVisibility(View.VISIBLE);
                // flower_sum.setText(++flower_count + "");
                ++flower_count;
                ++count;
                AnimationUtil.flowerAnimation(flower_copy[flower_img_position % 8]);
                flower_img_position++;
                timer = new Timer();

                timer.schedule(new TimerTask()
                {
                    int i = 0;

                    @Override
                    public void run()
                    {
                        Message msg = Message.obtain();
                        msg.what = i;
                        i += 4;
                        flowerHandler.sendMessage(msg);
                    }
                }, 4000, 1000);
                startFlowerTips.setText("请连续戳我");

            } else
            {
                flower_copy[flower_img_position % 8].setVisibility(View.VISIBLE);
                // flower_sum.setText(++flower_count + "");
                ++flower_count;
                ++count;
                AnimationUtil.flowerAnimation(flower_copy[flower_img_position % 8]);
                flower_img_position++;
            }
            // startFlowerTips.setVisibility(View.INVISIBLE);

            flowerStatusTips.setText(flower_count + "");
        }
    }

    Handler flowerHandler = new Handler()
    {

        public void handleMessage(android.os.Message msg)
        {

            TTLog.d("tag", "献花状态处于=====================" + msg.what);
            if (msg.what == (SEND_FLOWER_TIME))
            {
                flowerStatusTips.setText("时间到!");
                startFlowerTips.setText("");
                flowerClickable = false;
                isFlowerClick = false;
                flower.clearAnimation();

                if (timer != null)
                {
                    timer.cancel();
                    timer = null;
                }
                flowerHandler.sendMessageDelayed(flowerHandler.obtainMessage(REFRESH_FLOWER_STATUS), 500);

            } else if (msg.what == SEND_FLOWER_SUCCESS)
            {
                String autor = "";
                /*if (object_type.equals(PersonalNoteService.OBJECT_TYPE_NOTE))
                {
                    autor = "基友";

                } else if (object_type.equals(PersonalNoteService.OBJECT_TYPE_DRAW))
                {
                    autor = "画手";
                } else if (object_type.equals(PersonalNoteService.OBJECT_TYPE_COSER))
                {
                    autor = "coser";
                }*/
                //DiaobaoUtil.flowerFinish(flowerStatusTips, flower_count, autor);
                DiaobaoUtil.flowerFinishCP(flowerStatusTips, flower_count, autor);
                SpannableString textSpan = new SpannableString("扣5个豆子");
                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#f96d6d"));

                try
                {
                    Pattern p = Pattern.compile("5");
                    Matcher m = p.matcher("扣5个豆子");
                    while (m.find())
                    {
                        int start = m.start();
                        int end = m.end();
                        textSpan.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

                } catch (PatternSyntaxException e)
                {
                    e.printStackTrace();
                }
                // flowerStatusTips.setText(textSpan);
                flower_sum.setText("" + (flowerSum + flower_count));
                AnimationUtil.starAlphaAnimation(flowerIngUserImg, 3000);
                AnimationUtil.starAlphaAnimation(flowerStatusTips, 3000);
                flower_count = 0;
                count = 0;
                flowerHandler.sendMessageDelayed(flowerHandler.obtainMessage(RESET_CLICK_FLOWER), 4000);

            } else if (msg.what == SEND_FLOWER_FAIL)
            {
                flowerStatusTips.setText((String) msg.obj);
                flower_sum.setText("" + (flowerSum + flower_count - count));
                AnimationUtil.starAlphaAnimation(flowerIngUserImg, 1000);
                AnimationUtil.starAlphaAnimation(flowerStatusTips, 1000);
                flower_count = 0;
                count = 0;
                flowerHandler.sendMessageDelayed(flowerHandler.obtainMessage(RESET_CLICK_FLOWER), 4000);

            } else if (msg.what == REFRESH_FLOWER_STATUS)
            {// 献花结束，刷新
                /** 提交献花数量 */
                PersonalNoteService.getInstance().giveFlowerToNote(NewCpDesActivity.this, cpBean.getId(), Constant.OBJECT_TYPE_CP + "", String.valueOf(count));
                /** 刷新排名 */
                PersonalNoteService.getInstance().loadFlowerList(NewCpDesActivity.this, cpBean.getId(), Constant.OBJECT_TYPE_CP + "", "");

            } else if (msg.what == RESET_CLICK_FLOWER)
            {// 恢复献花按钮
                flowerClickable = true;
                flowerStatusTips.setText("");
                // startFlowerTips.setVisibility(View.VISIBLE);
                startFlowerTips.setText("CP要人气，戳菊请用力");
            }

        }
    };
    private PopupWindow mMorePopupWindow;

    private void showMorePopupWindow()
    {
        if (cpBean == null)
        {
            return;
        }
        if (mMorePopupWindow == null || mMoreView == null)
        {
            makeTopMorePopupWindow();
        }
        if (order_bt_cp.getVisibility() == View.VISIBLE)
        {
            cacel_line.setVisibility(View.GONE);
            collectLy.setVisibility(View.GONE);
        } else
        {
            cacel_line.setVisibility(View.VISIBLE);
            collectLy.setVisibility(View.VISIBLE);
        }
        mMorePopupWindow.showAtLocation(mMoreView, Gravity.TOP|Gravity.RIGHT , 0, 0);
        mMorePopupWindow.setFocusable(true);
        mMorePopupWindow.setOutsideTouchable(true);
        mMorePopupWindow.update();
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        if (response == null)
        {
            return;
        }
        if (waitDialog != null)
        {
            waitDialog.dismiss();
        }
        if (handleType == PersonalNoteService.HANDLE_TYPE_GIVE_FLOWER)
        {

            if (response.getStatus() == Constant.STATUS_SUCCESS)
            {
                flowerHandler.sendEmptyMessage(SEND_FLOWER_SUCCESS);
            } else
            {
                if (response.getUser_msg() != null)
                {
                    UIUtil.toastMessage(NewCpDesActivity.this, response.getUser_msg());
                } else
                {
                    UIUtil.toastMessage(NewCpDesActivity.this, Constant.loadingFail);
                }
                Message mes = Message.obtain();
                mes.obj = DiaobaoUtil.getErrorTips(response).trim();
                mes.what = SEND_FLOWER_FAIL;
                flowerHandler.sendMessage(mes);
            }
             /* if (response.getStatus() == 25)
            {
                if (response.getUser_msg() != null)
                {
                    //UIUtil.toastMessage(mContext, response.getUser_msg());
                    getDouziLayout.setVisibility(View.VISIBLE);
                    douziTextDes.setText(response.getUser_msg());
                } else
                {
                    UIUtil.toastMessage(NewCpDesActivity.this, Constant.loadingFail);
                }
            }*/
        } else
        {
            if (response.getStatus() == 0)
                switch (handleType)
                {
                    case PersonalNoteService.HANDLE_TYPE_LOAD_FLOWER_LIST:
                    {
                        final JSONObject jsonObject = (JSONObject) response.getData();
                        List<SweepFlowerBean> mainList;
                        try
                        {
                            if (jsonObject.get("list") instanceof JSONArray)
                            {
                                mainList = new ArrayList<SweepFlowerBean>();
                                JSONArray mainArray = jsonObject.getJSONArray("list");
                                SweepFlowerBean contentBean = null;
                                for (int i = 0; i < mainArray.length(); i++)
                                {
                                    contentBean = new SweepFlowerBean();
                                    if (mainArray.get(i) instanceof JSONObject)
                                    {
                                        contentBean.setJson(mainArray.getJSONObject(i));
                                    }
                                    mainList.add(contentBean);
                                }

                                if (mainList.size() != 0)
                                {
                                    no_flower.setVisibility(View.GONE);
                                    flowerUserListDatas.clear();
                                    flowerUserListDatas.addAll(mainList);
                                    flowerUserAdapter.notifyDataSetChanged();
                                    popu_flowerUserAdapter.notifyDataSetChanged();
                                    try
                                    {
                                        flowerSum = Integer.valueOf(jsonObject.getString("flowerCount"));
                                        flower_sum.setText(flowerSum + "");
                                        cp_flowernum_tv.setText(flowerSum + "");
                                    } catch (Exception e)
                                    {

                                    }
                                } else
                                {
                                    no_flower.setVisibility(View.VISIBLE);
                                    flower_sum.setText("0");
                                }
                            }

                        } catch (JSONException e)
                        {
                            e.printStackTrace();
                        }

                        break;
                    }
                    case CollectService.HANDLE_TYPE_ADD_TAG_COLLECT:
                    case CpService.HANDLE_TYPE_ADD_CP:
                        //waitDialog.dismiss();
                        if (response.getStatus() == Constant.STATUS_SUCCESS)
                        {
                            UIUtil.toastMessage(this, "订阅成“攻”");
                            cp_flowerUserList.setVisibility(View.VISIBLE);
                            order_bt_cp.setVisibility(View.GONE);
                        } else
                        {
                            UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
                        }
                        cpBean.setFollowing("1");
                        break;
                    case CpService.HANDLE_TYPE_CANCEL_CP:
                    case CollectService.HANDLE_TYPE_REMOVE_TAG_COLLECT:
                        if (response.getStatus() == Constant.STATUS_SUCCESS && response.getFail_code() == 0)
                        {
                            UIUtil.toastMessage(this, "已取消");
                            cp_flowerUserList.setVisibility(View.GONE);
                            order_bt_cp.setVisibility(View.VISIBLE);

                        } else
                        {
                            UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
                        }
                        cpBean.setFollowing("0");
                        break;
                    // 举报
                    case HANDLE_LOAD_REPORT_INFO:
                        UIUtil.toastMessage(this, "我们将尽快审核，请继续愉快地玩耍吧！");
                        break;
                    case CpService.HANDLE_TYPE_LOAD_SINGLE_CP:
                    {
                        if (response.getStatus() == Constant.STATUS_SUCCESS)
                        {
                            final JSONObject jsonObject = (JSONObject) response.getData();
                            cpBean = new CpBean();
                            try
                            {
                                JSONObject tag_jsonObject = jsonObject.getJSONObject("tag");
                                if (tag_jsonObject != null)
                                {
                                    cpBean.setJson2(tag_jsonObject);
                                    setCpInfo();
                                    initUpView();
                                }
                            } catch (JSONException e)
                            {
                                e.printStackTrace();
                            }
                        } else
                        {
                            UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
                        }
                        break;
                    }
                }
            else
            {
                if (response.getUser_msg() != null)
                {
                    UIUtil.toastMessage(this, response.getUser_msg());
                } else
                {
                    UIUtil.toastMessage(this, Constant.loadingFail);
                }
            }
        }
    }

}